package com.tsinghuabigdata.edu.ddmath.requestHandler.requestImpl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tsinghuabigdata.edu.commons.http.HttpRequest;
import com.tsinghuabigdata.edu.commons.http.HttpRequestException;
import com.tsinghuabigdata.edu.ddmath.bean.ClassInfo;
import com.tsinghuabigdata.edu.ddmath.bean.DoudouWork;
import com.tsinghuabigdata.edu.ddmath.bean.MyCourse;
import com.tsinghuabigdata.edu.ddmath.commons.http.AppRequest;
import com.tsinghuabigdata.edu.ddmath.commons.http.AppRequestUtils;
import com.tsinghuabigdata.edu.ddmath.constant.AppConst;
import com.tsinghuabigdata.edu.ddmath.constant.AppRequestConst;
import com.tsinghuabigdata.edu.ddmath.constant.ErrTag;
import com.tsinghuabigdata.edu.ddmath.module.ddwork.bean.DDWorkDetail;
import com.tsinghuabigdata.edu.ddmath.module.mylearn.bean.WorkBean;
import com.tsinghuabigdata.edu.ddmath.requestHandler.MyLearnService;
import com.tsinghuabigdata.edu.ddmath.util.AppLog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


public class MyLearnServiceImpl extends BaseService implements MyLearnService {

    @Override
    public String uploadImage(String fileId, String accessToken, InputStream stream) throws HttpRequestException, UnsupportedEncodingException, JSONException {

        String url = getUrl(AppRequestConst.POST_MYLEARN_UPLOADIMAGE);
        AppLog.d("MyLearnServiceImpl upload url = " + url);

        HttpRequest request = AppRequestUtils.post(url).setTimeout(200);
        request.putHeader("access_token", accessToken)
                .putFrom("fileId", fileId)
                .putFrom("file", stream, "file" + AppConst.IMAGE_SUFFIX_NAME)
                .upload();
        String res = request.getBody();
        AppLog.d("MyLearnServiceImpl upload image = " + res);

        JSONObject json = new JSONObject(res);
        return json.getString("path");
    }

    @Override
    public JSONArray removeImages( String accessToken, JSONArray files ) throws HttpRequestException, UnsupportedEncodingException, JSONException{
        String url = getUrl(AppRequestConst.POST_MYLEARN_REMOVEIMAGE);
        AppLog.d("MyLearnServiceImpl remove images url = " + url);

        HttpRequest request = AppRequestUtils.post(url).setTimeout(200);
        request.putHeader("access_token", accessToken);

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("files", files);
        } catch (Exception e) {
            AppLog.i(ErrTag.TAG_JSON, e.toString());
        }
        request.setJsonStringParams( jsonObject.toString() );
        String res = request.upload().getBody();
        AppLog.d("MyLearnServiceImpl upload image = " + res);

        return new JSONArray(res);
    }

    @Override
    public String submitWorkInfo(String access_token, String studentId, String classId, int questionCount, JSONArray jsonArray) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.POST_MYLEARN_WORKINFO);
        AppLog.d("xxxbook upload question url = " + url);

        HttpRequest request = AppRequestUtils.post(url);
        request.putHeader("access_token", access_token).setTimeout(5 * 60);

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("studentId", studentId);
            jsonObject.put("classId", classId);
            jsonObject.put("questionCount", questionCount);
            jsonObject.put("files", jsonArray);
        } catch (Exception e) {
            AppLog.i(ErrTag.TAG_JSON, e.toString());
        }
        request.setJsonStringParams(jsonObject.toString());
        return request.requestJson().getBody();
    }

    @Override
    public String submitDDWorkInfo(String access_token, String studentId, String classId, String examId, JSONArray pages) throws HttpRequestException, JSONException{
        String url = getUrl(AppRequestConst.POST_MYLEARN_DDWORKINFO);
        AppLog.d("xxxbook upload question url = " + url);

        HttpRequest request = AppRequestUtils.post(url);
        request.putHeader("access_token", access_token).setTimeout(100);

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("studentId", studentId);
            jsonObject.put("classId", classId);
            jsonObject.put("examId", examId);
            jsonObject.put("pages", pages);
        } catch (Exception e) {
            AppLog.i(ErrTag.TAG_JSON, e.toString());
        }
        request.setJsonStringParams(jsonObject.toString());
        return ((AppRequest)request.requestJson()).getDataBody();
    }

    @Override
    public ArrayList<WorkBean> getWorkList(String access_token, String studentId, String classId, String date) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.GET_MYLEARN_WORKLIST) + "&studentId=" + studentId + "&classId=" + classId + "&date=" + date;
        AppLog.d("xxxbook upload getWorkList url = " + url);
        HttpRequest request = AppRequestUtils.get(url);
        request.putHeader("access_token", access_token);
        String res = request.requestJson().getBody();
        AppLog.d("xxxbook  getWorkList res = " + res);
        return new Gson().fromJson(res, new TypeToken<ArrayList<WorkBean>>() {
        }.getType());
    }

    @Override
    public DDWorkDetail getDDWorkDetail(String access_token, String studentId, String workId ) throws HttpRequestException, JSONException {

        String url = getUrl(AppRequestConst.GET_MYLEARN_DDWORK_DETAIL) + "&workId=" + workId;
        HttpRequest request = AppRequestUtils.get(url);
        request.putHeader("access_token", access_token)
               .putRestfulParam("studentId", studentId);
        String res = request.requestJson().getBody();
        AppLog.d("xxxbook  getWorkList res = " + res);
        return new Gson().fromJson( res, new TypeToken<DDWorkDetail>() {
        }.getType());
    }

    @Override
    public List<MyCourse> myCourse(String access_token, String studentId, String classId, int pageNum, int pageSize) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.GET_MY_COURSE) + "&studentId=" + studentId + "&classId=" + classId + "&pageNum=" + pageNum + "&pageSize=" + pageSize;
        AppLog.d("myCourse url = " + url);
        HttpRequest request = AppRequestUtils.get(url);
        request.putHeader("access_token", access_token);
        String res = request.requestJson().getBody();
        AppLog.d("myCourse res = " + res);
        return new Gson().fromJson(res, new TypeToken<List<MyCourse>>() {
        }.getType());
    }

    @Override
    public ClassInfo queryClassInfo(String access_token, String classId) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.QUERY_CLASS_INFO);
        HttpRequest request = AppRequestUtils.get(url);
        request.putHeader("access_token", access_token)
                .putRestfulParam("classId", classId);
        String res = request.requestJson().getBody();
        AppLog.d("queryClassInfo res = " + res);
        return new Gson().fromJson(res, ClassInfo.class);
    }

    @Override
    public DoudouWork queryDoudouWork(String access_token, String studentId, int pageNum, int pageSize, int status) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.QUERY_DOUDOU_WORK);
        HttpRequest request = AppRequestUtils.get(url);
        request.putHeader("access_token", access_token)
                .putRestfulParam("studentId", studentId)
                .putRequestParam("pageNum", pageNum+"")
                .putRequestParam("pageSize", pageSize+"")
                .putRequestParam("status", status+"");
        String res = request.request().getBody();
        return new Gson().fromJson(res, DoudouWork.class);
    }

}
